from openpyxl import load_workbook
from zhipuai import ZhipuAI
import re
import os

client = ZhipuAI(api_key="e896525e15c4856390a089cc7e199c46.NU3hO2WLngbwkXmV")


def add_value(zyi_value,wb):
    ws = wb["Product Content And Site Exp"]

    # 获取最大行号
    max_row = ws.max_row
    AA7 = ws['AA7'].value # 最小码
    AA_max = ws[f'AA{max_row}'].value # 最大码


    # 计算最大行号减去6
    intzyi = max_row - 6

    for i in range(0,intzyi):
        ws[f'H{7+i}'] = "AAAA women "+zyi_value + f" {AA7}-{AA_max}"


def add_data(zyi_list,wb):
    ws = wb["Product Content And Site Exp"]

    # 获取最大行号
    max_row = ws.max_row

    # 计算最大行号减去6
    intzyi= max_row-6

    for i in range(0,intzyi):

        ws[f'N{7+i}'] = zyi_list[0]
        ws[f'O{7+i}'] = zyi_list[1]
        ws[f'P{7+i}'] = zyi_list[2]
        ws[f'Q{7+i}'] = zyi_list[3]
        ws[f'R{7+i}'] = zyi_list[4]


    wb.save(file101) # 保存

def GLM(file101):
    wb = load_workbook(file101)
    # 选择工作表
    ws = wb["Product Content And Site Exp"]
    dict_str = ws['C7'].value
    if dict_str == None:
        print("找不到产品资料")
        return None
    else:
        ws['C7'] = None
        respons = client.chat.completions.create(
            model="glm-4",  # 请填写您要调用的模型名称
            messages=[
                {"role": "user", "content": f"你是一名资深的亚马逊女士服装类目文案专家，f{dict_str}这个是你的产品资料"},
                {"role": "user", "content": f"要求：1、吸引人的注意力，整个标题语言生动有感染力能激发消费者的购买意欲。2、包含40至55个字符。3、绝对不能出现价格"},
                {"role": "user", "content": "根据资料和要求写一个产品标题，并翻译成英文保存到变量zyi"},

            ],
        )
        response_str = respons.choices[0].message.content
        print("（产品标题）原参数为：", respons.choices[0].message)
        rege = r'zyi = "(.*?)"'
        # 使用re.search查找匹配的字符串
        matc = re.search(rege, response_str, re.DOTALL)
        # 如果找到匹配项，则提取数组内容
        if matc:
            zyi_value = matc.group(1)
            add_value(zyi_value, wb)
            print("产品标题为：", zyi_value)
        else:
            print("没有找到匹配的产品标题为")



        response = client.chat.completions.create(
            model="glm-4",  # 请填写您要调用的模型名称
            messages=[
                {"role": "user", "content": f"你现在是一个服装卖家，f{dict_str}这个是你的产品资料"},
                {"role": "user", "content": "根据资料写卖点一：面料，卖点二：设计卖点，卖点三：搭配，卖点四：场合，卖点五：服装护理"},
                {"role": "user", "content": "把这五个卖点每个都翻译成英文并保存在zyi数组中，以Python数组形式返回，不要注释和多维数组"},

            ],
        )
        response_str = response.choices[0].message.content
        print("（卖点）原参数为：", response.choices[0].message)
        regex = r"zyi = \[(.*?)\]"
        # 使用re.search查找匹配的字符串
        match = re.search(regex, response_str, re.DOTALL)

        # 如果找到匹配项，则提取数组内容
        if match:
            list_str = match.group(1)

            # # 移除字符串中的多余空格和换行符
            list_str = re.sub(r'\s+', ' ', list_str).strip()
            # 将字符串转换为Python数组
            zyi_list = [item.strip("") for item in list_str.split('", "')]

            print("数组为：", zyi_list)
            add_data(zyi_list,wb)
            return zyi_list
        else:
            print("没有找到匹配的数组")
            return None




if __name__ == '__main__':
    # zyi_list = ['"Fabric: Made from 100% Polyester, this Cinnamon Solid Front Pockets Sleeveless Corduroy Overall Dress offers a durable and comfortable corduroy fabric perfect for everyday wear.', 'Design: The timeless solid color palette and practical front pockets make this dress both stylish and functional, while the sleeveless design is ideal for warm weather.', 'Outfit Pairing: This casual shift silhouette mini dress can be easily paired with a variety of accessories, making it a versatile addition to your wardrobe.', 'Occasion: Perfect for daily wear, this dress is suitable for a range of casual occasions, offering both comfort and style.', 'Care: With proper care, this 100% Polyester dress can maintain its quality and color, ensuring it remains a favorite in your wardrobe."']
    # add_data()
    # dinam()

    # 指定路径
    path = r"\\192.168.50.101\沃尔玛铺货资料a\铺货资料文件\沃尔玛表"

    # 使用os.walk()遍历目录
    for root, dirs, files in os.walk(path):
        for file in files:
            # 构建文件的完整路径
            file101 = os.path.join(root, file)
            ifs = GLM(file101)
            if ifs == None:
                continue
            else:
                print(file101, "写入成功")
